Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    swiperTab: [
      {
        text: '全部订单',
      },
      {
        text: '待付款',
      },
      {
        text: '待收货',
      },
    ],

    swiperTabIndex: 0,
    Hei: '',
    allorderList: [

    ],

    paymentList: [

    ],

    receviedList: [

    ]



  },

  // 顶部导航的点击事件
  changeTab: function (event) {
    // console.log(event)
    this.setData({
      swiperTabIndex: event.target.dataset.index
    })
  },

  // 内容的切换事件
  changeContent: function (event) {
    // console.log(event)
    this.setData({
      swiperTabIndex: event.detail.current
    })
  },


  // 待收货的按钮的点击事件
  receviedbtn: function (event) {
    console.log(event)
    var index = event.target.dataset.index
    // 获取当前点击的商品数据
    var receviedList = wx.getStorageSync('receviedList')
    var allorderList = wx.getStorageSync('allorderList')
    var good = receviedList[index]
    // console.log("1==>"good)
    // 修改按钮的文本为“已收货”
    good.btn = '删除'
    // 在待收货的缓存数据中删除当前数据
    // console.log("2==>"good)
    receviedList.splice(index, 1)
    wx.setStorageSync('receviedList', receviedList)
    // 修改全部订单中的当前商品文本为“已收货”，将当前商品放到列表的最前面
    for (var i = 0; i < allorderList.length; i++) {
      if (good.number == allorderList[i].number) {
        allorderList.splice(i, 1)
        if (allorderList.length == 0) {
          wx.setStorageSync('allorderList', [good])
        } else {
          allorderList.unshift(good)
          wx.setStorageSync('allorderList', allorderList)
        }
      }
    }
    // 渲染页面
    this.setData({
      receviedList: wx.getStorageSync('receviedList'),
      allorderList: wx.getStorageSync('allorderList'),
    })
  },

  // 待付款的按钮的点击事件
  paymentbtn: function (event) {
    var index = event.target.dataset.index
    // 获取当前点击的商品数据
    var paymentList = wx.getStorageSync('paymentList')
    var receviedList = wx.getStorageSync('receviedList')
    var allorderList = wx.getStorageSync('allorderList')
    var good = paymentList[index]
    // console.log("1==>"good)
    // 修改按钮的文本为“确认收货”
    good.btn = '确认收货'
    // 在待付款的缓存数据中删除当前数据
    // console.log("2==>"good)
    paymentList.splice(index, 1)
    wx.setStorageSync('paymentList', paymentList)
    // 将当前数据添加到待收货的缓存数据中
    if (receviedList.length == 0) {
      wx.setStorageSync('receviedList', [good])
    } else {
      receviedList.unshift(good)
      wx.setStorageSync('receviedList', receviedList)
    }
    // 修改全部订单中的当前商品文本为“已收货”，将当前商品放到列表的最前面
    for (var i = 0; i < allorderList.length; i++) {
      if (good.number == allorderList[i].number) {
        allorderList.splice(i, 1)
        if (allorderList.length == 0) {
          wx.setStorageSync('allorderList', [good])
        } else {
          allorderList.unshift(good)
          wx.setStorageSync('allorderList', allorderList)
        }
      }
    }
    console.log(wx.getStorageSync('receviedList'))
    // 渲染页面
    this.setData({
      receviedList: wx.getStorageSync('receviedList'),
      allorderList: wx.getStorageSync('allorderList'),
      paymentList: wx.getStorageSync('paymentList')
    })
  },

  // 全部订单的按钮的点击事件
  allorderbtn: function (event) {
    var index = event.target.dataset.index
    // 获取当前点击的商品数据
    var allorderList = wx.getStorageSync('allorderList')
    var paymentList = wx.getStorageSync('paymentList')
    var receviedList = wx.getStorageSync('receviedList')
    var good = allorderList[index]
    // 判断按钮文本，确定当前商品在待付款还是待收货中
    if (good.btn == '去付款') {
      // 修改按钮文本为“确认收货”
      good.btn = '确认收货'
      // 在全部订单中删除当前商品，并重新插入到头部
      allorderList.splice(index,1)
      allorderList.unshift(good)
      wx.setStorageSync('allorderList', allorderList)
      // 在待付款中删除相对应的商品
      for(var i=0;i<paymentList.length;i++){
        if(good.number == paymentList[i].number){
          paymentList.splice(i,1)
        }
      }
      wx.setStorageSync('paymentList', paymentList)
      // 在待收货中添加该商品
      if(receviedList.length == 0){
        wx.setStorageSync('receviedList', [good])
      }else{
        receviedList.unshift(good)
        wx.setStorageSync('receviedList', receviedList)
      }
    } else if (good.btn == '确认收货') {
      // 修改按钮文本为“删除”
      good.btn = '删除'
      // 在全部订单中删除当前商品，并重新插入到头部
      allorderList.splice(index,1)
      allorderList.unshift(good)
      wx.setStorageSync('allorderList', allorderList)
      // 在待付款中删除相对应的商品
      for(var i=0;i<receviedList.length;i++){
        if(good.number == receviedList[i].number){
          receviedList.splice(i,1)
        }
      }
      wx.setStorageSync('receviedList', receviedList)
    }else{
      // 在全部订单中删除该商品
      allorderList.splice(index,1)
      wx.setStorageSync('allorderList', allorderList)
    }
    // 渲染页面
    this.setData({
      allorderList:wx.getStorageSync('allorderList'),
      receviedList:wx.getStorageSync('receviedList'),
      paymentList:wx.getStorageSync('paymentList')
    })
  },















  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      swiperTabIndex: options.index,
      Hei: wx.getSystemInfoSync().windowHeight

    })

  },

  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      allorderList: wx.getStorageSync('allorderList'),
      paymentList: wx.getStorageSync('paymentList'),
      receviedList: wx.getStorageSync('receviedList')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/personal/personal',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }


}) 